package com.ruoyi.base.controller;

import java.util.List;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;

import cn.hutool.core.lang.tree.Tree;
import com.ruoyi.base.domain.SyQj;
import com.ruoyi.base.domain.SyYj;
import com.ruoyi.base.domain.bo.SyTypeBo;
import com.ruoyi.base.domain.vo.SyTypeVo;
import com.ruoyi.common.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckLogin;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.base.domain.vo.SyQjVo;
import com.ruoyi.base.domain.bo.SyQjBo;
import com.ruoyi.base.service.ISyQjService;

/**
 * 试验器具
 *
 * @author zyb
 * @date 2024-04-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/base/syQj")
public class SyQjController extends BaseController {

    private final ISyQjService iSyQjService;

    /**
     * 查询试验器具列表
     */
    @SaCheckLogin
    @GetMapping("/list")
    public R<List<SyQjVo>> list(SyQjBo bo) {
        List<SyQjVo> list = iSyQjService.queryList(bo);
        return R.ok(list);
    }

    /**
     * 导出试验器具列表
     */
    @SaCheckLogin
    @Log(title = "试验器具", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(SyQjBo bo, HttpServletResponse response) {
        List<SyQjVo> list = iSyQjService.queryList(bo);
        ExcelUtil.exportExcel(list, "试验器具", SyQjVo.class, response);
    }

    /**
     * 获取试验器具详细信息
     *
     * @param syQjId 主键
     */
    @SaCheckLogin
    @GetMapping("/{syQjId}")
    public R<SyQjVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long syQjId) {
        return R.ok(iSyQjService.queryById(syQjId));
    }

    /**
     * 新增试验器具
     */
    @SaCheckLogin
    @Log(title = "试验器具", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody SyQjBo bo) {
        return toAjax(iSyQjService.insertByBo(bo));
    }

    /**
     * 修改试验器具
     */
    @SaCheckLogin
    @Log(title = "试验器具", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody SyQjBo bo) {
        return toAjax(iSyQjService.updateByBo(bo));
    }

    /**
     * 删除试验器具
     *
     * @param syQjIds 主键串
     */
    @SaCheckLogin
    @Log(title = "试验器具", businessType = BusinessType.DELETE)
    @DeleteMapping("/{syQjIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] syQjIds) {
        return toAjax(iSyQjService.deleteWithValidByIds(Arrays.asList(syQjIds), true));
    }

    /**
     * 获取器具树列表
     */
    @SaCheckLogin
    @GetMapping("/qjTree")
    public R<List<Tree<Long>>> qjTree(SyQj qj) {
        return R.ok(iSyQjService.selectQjTreeList(qj));
    }


    /**
     * 查询子集
     */
    @SaCheckLogin
    @GetMapping("/pageSub")
    public TableDataInfo<SyQjVo> pageSub(SyQjBo bo, PageQuery pageQuery) {
        return iSyQjService.pageSub(bo, pageQuery);
    }

    /**
     * 器具列表
     */
    @SaCheckLogin
    @GetMapping("/qjList")
    public R<List<SyQjVo>> qjList(SyQjBo bo) {
        return R.ok(iSyQjService.qjList(bo));
    }
}
